var order = function(el, path, replacement) {
	
	if($('.order').length) return false;
	
	var self = $(el);
	var index = $('.offer').index(self.parent().parent());
	
	$.get(path, function(data) {
		
		var orderElement = $('<div class="order"></div>').html(data).append('<div class="order-close roll-up"></div>');
		var clearStart = $('<div class="clear"></div>');
		var clearEnd = $('<div class="clear"></div>');
		
		var changeElement = $('.offer').eq((!((index + 1) % 2))? index:(index+1));
		
		changeElement.after( orderElement ).after( clearStart );
		orderElement.after( clearEnd );
		
		var _width = orderElement.width() - 30;
		var _height = orderElement.height() + 15;
		
		orderElement.css({ 'width': 0, 'height': 0 });
		
		if(!((index + 1) % 2)) orderElement.css('float', 'right');
		
		orderElement.animate({
			width: _width,
			height: _height
		}, 1000);
		
		$('body, html').animate({ scrollTop: orderElement.offset().top - 100 }, 1000);
		
		orderElement.find('.order-close').click(function() {
			
			orderElement.fadeOut('slow', function() {
				
				$(this, clearStart, clearEnd).remove();
			});
		});
		
	});
	
	self.removeClass('btns').text(replacement);
	
	return false;
}

var openDialog = function(heading, p) {
	
	var dialogWrap = $('<div class="dialog-wrap"></div>').appendTo('body').css({
		width: $(window).width(),
		height: $(window).height()
	}).fadeTo('slow', 0.8);
	
	var html = '<div class="dialog-heading"><button type="button" class="close">×</button><h3>' + heading + '</h3></div><div class="dialog-body">' + p.html + '</div><div class="dialog-footer"><button class="btn btn-close">' + p.close + '</button><button class="btn btn-primary">' + p.save + '</button></div>';
	
	var dialog = $('<div class="dialog"></div>').appendTo('body').html(html);
	
	var _top = $(window).height()/2 - dialog.height()/2;
	var _dialogCss = { left: $(window).width()/2 - dialog.width()/2, top: -(dialog.height()), opacity: 0 };
	
	dialog.css(_dialogCss).animate({ top: _top, opacity: 1 }, 'slow');
	
	var close = function() {
		dialog.animate({ top: -(dialog.height()), opacity: 0 }, 'slow', function() {
			
			$(this).remove();
			dialogWrap.fadeOut('slow', function() { $(this).remove(); });			
		});
	}
	
	dialog.find('.close').add('.dialog .btn-close').click(close);
	
	return false;
}

var loader = function(el, p) {
	
	$(el).html('<img src=\'images/loader.gif\' style="margin:13px 0 0 0;">');
	
	$.get(p.path, function(data) {
		$('.offers').html( $('.offers').html() + data );
		$(el).html('Show more');
	});
	
	return false;
}

 

var jalert = function(heading, text, p) {
	
	if($('.alert').length) return false;
	
	var _jalert = $('<div class="alert"></div>').appendTo('body');
	
	_jalert.html('<p>' + heading + '</p><p>' + text + '</p><p><a class="btn btn-danger" href="#">' + p.btn1 + '</a> <a class="btn" href="#">' + p.btn2 + '</a></p>');
	
	_jalert.css({
		left: $(window).width()/2 - _jalert.width()/2,
		top: $(window).height()/2 - _jalert.height()/2,
		opacity: 0
	}).animate({ opacity: 1 }, 'slow');
	
	_jalert.find('.close').click(function() {
		_jalert.remove();
	});
	
	return false;
}


/* input value hide onclick - прячем значение по умолчанию по фокусу в поле input и textarea */

$ (function(){
	$('input:text').each(function(){
		var txtval = $(this).val();
		$(this).focus(function(){
			$(this).val('')
		});
		$(this).blur(function(){
			if($(this).val() == ""){
				$(this).val(txtval);
			}
		});
	});
});

$ (function(){
	$('textarea').each(function(){
		var txtval = $(this).val();
		$(this).focus(function(){
			$(this).val('')
		});
		$(this).blur(function(){
			if($(this).val() == ""){
				$(this).val(txtval);
			}
		});
	});
});
